<?php
/**
 * Created by PhpStorm.
 * Power By Mikkle
 * Email：776329498@qq.com
 * Date: 2017/9/15
 * Time: 17:57
 */

namespace mikkle\tp_wechat;


use mikkle\tp_wechat\base\BaseWechatApi;

/**
 * 微信信息同送接口操作类
 * 推荐继承或者复制本类进行修改即可
 * Power: Mikkle
 * Email：776329498@qq.com
 * Class WechatApi
 * @package mikkle\tp_wechat
 */
class WechatApi extends BaseWechatApi
{
    public function __construct()
    {
        parent::__construct();

        $this->cdn = config('cdn');
     
    }
    /**
     * 默认文本消息回复内容
     *
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageText()
    {
        //接收用户发送过来的关键词
        $keyword = $this->weObj->getRevContent();

        //广州天气   城市+天气
        preg_match("/(.*)天气$/", $keyword, $result);

        $city = isset($result[1]) ? $result[1] : "";

        if(!empty($city))
        {
            //参数编码转化
            $city = urlencode($city);

            //组装接口地址
            $url = "https://free-api.heweather.net/s6/weather/now?location=$city&key=db86a5196f304e52a4369818c5182e60";

            $json = file_get_contents($url);

            if(empty($json))
            {
                return '查询结果不存在';
            }

            $data = json_decode($json, true);

            if(!is_array($data) || empty($data))
            {
                return '暂无天气结果';
            }

            $info = $data['HeWeather6'][0];

            //城市
            $city = $info['basic']['location'];

            // 日期
            $date = $info['update']['loc'];

            // 温度
            $tmp = $info['now']['tmp'];

            //天气描述
            $cond_txt = $info['now']['cond_txt'];

            //风向
            $wind_dir = $info['now']['wind_dir'];

            $message = "天气查询结果：\n城市：$city\n日期：$date\n温度：{$tmp}℃\n天气描述：$cond_txt\n风向：$wind_dir";

            return $message;
        }

        //将中文关键词进行转换
        $keyword = urlencode($keyword);

        $url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=$keyword";

        $json = file_get_contents($url);

        $array = json_decode($json, true);

        $content = isset($array['content']) ? $array['content'] : '暂时听不懂你说啥';

        $content = trim(strip_tags($content));

        $this->wedump($content);
        exit;
    }

    /**
     * 默认图片信息回复内容
     *
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnMessageImage($data = [])
    {
        $newsarray = [
            [
                'Title' => '你的图片发送成功',
                'Description' => '这是你发的图片吧',
                'PicUrl' => $this->data['PicUrl'],
                'Url' => $this->request->domain(),
            ],
        ];
        $reply = ['type' => 'news', 'message' => $newsarray];
        return $reply;
    }

    /**
     * 默认语音信息回复内容处理方法
     *
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageVoice()
    {
        if (!empty($this->data['Content'])) {
            $keyword = $this->data['Content'];  //TODO::语音识别 语义分析
        } else {
            $keyword = $this->data->type;
        }
        $reply = '发送的语音翻译是:' . $keyword;
        return $reply;
    }

    /**
     * 默认音乐信息回复内容处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageMusic()
    {
        return '发送的是音乐';
    }

    /**
     * 默认视频信息回复处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageVideo()
    {
        return '发送的是视频';
    }

    /**
     * 默认发送地理位置回复信息处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageLocation()
    {
        $info = $this->weObj->getRevGeo();

        $x = $info['x']; //纬度
        $y = $info['y']; //经度
        $label = $info['label']; //详细地址

        //秘钥
        $ak = "4b3ec9dde16e3f7ed86dce81a4e9578d";

        //请求地址
        $url = "https://api.map.baidu.com/place/v2/search?query=美食&location=$x,$y&radius=5000&output=json&page_size=3&scope=2&ak=${ak}";

        //发送请求
        $json = file_get_contents($url);

        //获取结果
        $arr = json_decode($json, true);

        $results = isset($arr['results']) ? $arr['results'] : [];

        $datalist = [];

        if(!empty($results))
        {
            foreach($results as $item)
            {
                $overall_rating = $item['detail_info']['overall_rating'];
                $pic = $this->cdn."/static/logo.jpg";
                $datalist[] = [
                    'Title' => $item['name']." (评分:$overall_rating)",
                    'Description' => $item['address'],
                    'PicUrl' => $pic,
                    'Url' => $item['detail_info']['detail_url']
                ];
            }
        }

        if(empty($datalist))
        {
            return '暂无搜索到美食信息';
        }else
        {
            return $this->returnMessageImage($datalist);
        }
    }

    /**
     * 默认链接回复内容处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnMessageLink()
    {
        return '发送的是链接';
    }

    /**
     * 默认关注回复处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnEventSubscribe()
    {
        $message = "感谢你关注“ASK云服务”，使用帮助\n1、发送城市名称，可搜索天气\n2、发送预约单号，可搜索预约进度\n3、发送商品订单号，可搜索商品订单进度\n4、发送悬赏问题，可查看解决方案(提问：关键词)";
        return ['type' => 'text', 'message' => $message];
    }

    /**
     * 默认取消关注回复处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnEventUnsubscribe()
    {
        return ['type' => 'text', 'message' => '期待你的再次关注'];
    }

    /**
     * 默认扫码事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnEventScan()
    {
        $message = $this->weObj->getRevEvent();
        if (isset($message["key"])) {
            return ['type' => 'text', 'message' => "你的扫码成功,扫码内容:{$message['key']}"];
        }
        return ['type' => 'text', 'message' => '你的扫码成功'];
    }

    /**
     * 默认上报地理事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnEventLocation()
    {
        return ['type' => 'text', 'message' => '地理位置上报成功'];
    }

    /**
     * 默认点击菜单关键字处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return array
     */
    protected function returnEventClick()
    {
        $message = $this->weObj->getRevEvent();
        if (isset($message["key"])) {
            return ['type' => 'text', 'message' => "你的点击成功,点击关键字内容:{$message['key']}"];
        }
        return ['type' => 'text', 'message' => '你的点击成功'];
    }

    /**
     * 菜单调用扫码事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $type
     * @return array
     */
    protected function returnEventMenuScan($type)
    {
        $message = $this->weObj->getRevEvent();
        switch ($type) {
            case "push":  //推送
                if (isset($message["key"])) {
                    return ['type' => 'text', 'message' => "你的点击菜单扫码,关键字内容:{$message['key']}"];
                }
                return ['type' => 'text', 'message' => '你的点击菜单扫码成功'];
                break;
            case "waitmsg":
                //等候

                if (isset($message["key"])) {
                    return ['type' => 'text', 'message' => "你的点击菜单扫码等待中,关键字内容:{$message['key']}"];
                }
                return ['type' => 'text', 'message' => '你的点击菜单扫码等待中'];
                break;
            default:
                ;
        }
    }

    /**
     * 通过菜单上传图片处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $type // sys 系统 | photo  相册 | weixin 微信相册
     * @return array
     */
    protected function returnEventMenuPic($type)
    {
        $message = $this->weObj->getRevEvent();
        if (isset($message["key"])) {
            return ['type' => 'text', 'message' => "你的通过菜单上传图片成功,图片方式{$type},关键字内容:{$message['key']}"];
        }
        return ['type' => 'text', 'message' => "你的通过菜单上传图片成功,图片方式{$type}"];
    }


    /**
     * 菜单上报地理事件
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventMenuLocation()
    {
        return $reply['message'] = 'success';
    }

    /**
     * 群发成功推送结果处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventSendMass()
    {
        return $reply['message'] = 'success';
    }

    /**
     * 模版消息接收结果处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventSendTemplate()
    {
        return $reply['message'] = 'success';
    }

    /**
     * 客服事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $type //create | close |  switch
     * @return string
     */
    protected function returnEvenKfSession($type)
    {
        return $reply['message'] = 'success';
    }

    /**
     * 卡类事件 处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $type //pass | notpass | user_get |user_dell
     * @return string
     */
    protected function returnEventCard($type)
    {
        return $reply['message'] = 'success';
    }

    /**
     * wifi连一连处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventWifiConnected()
    {
        return $reply['message'] = 'success';
    }

    /**
     * 周围摇一摇事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventAroundUserSnake()
    {
        return $reply['message'] = 'success';
    }


    /**
     * 其他未知事件处理方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return string
     */
    protected function returnEventOthers()
    {
        return $reply['message'] = 'success';
    }

    protected function wedump($value)
    {
        ob_start();
        echo $value;
        $back = ob_get_clean();
        $this->weObj->text($back)->reply();
        ob_end_flush();
        die;
        return;
    }




}